<?php
/**
 * 权限管理
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;   
class AuthController extends AdminController {

	//权限列表
	public function index(){
		$this->module = M('module')->select();
		$m            = D('RuleView');
		$count        = $m->count();
		$page         = new \Library\Page($count,10);
		$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% <li>%HEADER%</li>');
		$show         = $page->show();
		$access       = $m->limit($page->firstRow.','.$page->listRows)->order('mid')->select();
		$this->assign('page',$show);
		$this->assign('access',$access);		
		$this->assign("title","权限管理");
		$this->display();
	}
	//添加权限处理
	public function addRule(){
		$rule=D("AuthRule");
		if (!$rule->create()){
			$this->error($rule->getError());
		}else{
			$data = I();
	        //过滤方法必须为空,否则验证时会出错
			$data['condition']=I('post.condition','','');
			$rule->add($data)?$this->success("添加成功"):$this->error("添加失败");
		}
	}
	//编辑权限
	public function editRule(){
		$id = I("get.id");
		if(empty($id))$this->error("缺少参数!");
		$this->assign("title","编辑权限");
		$this->rule = D('RuleView')->where("rule.id=".$id)->select();
		$this->module = M('module')->select();
		$this->display();
	}
	//编辑权限处理表单
	public function editRuleHandle(){
		if(!IS_POST)$this->error("请求方式错误!");	
		$rule=D("AuthRule");
		if (!$rule->create()){
			$this->error($rule->getError());
		}else{
			$data = I();
	        //过滤方法必须为空,否则验证时会出错
			$data['condition']=I('post.condition','','');
			$rule->save($data)?$this->success("更新成功"):$this->error("更新失败");
		}
	}
	//删除权限
	public function delRule(){
		$id = I("get.id");
		if(empty($id))$this->error("缺少参数!");	
		$result = D('AuthRule')->where("id=".$id)->delete();
		$result?$this->success("删除成功"):$this->error("删除失败");
	}
	//用户组列表
	public function group(){

		$group = M('auth_group');
		$count        = $group->count();
		$page         = new \Library\Page($count,10);
		$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% <li>%HEADER%</li>');
		$show         = $page->show();
		$list       = $group->limit($page->firstRow.','.$page->listRows)->order('id')->select();
		$this->assign('page',$show);
		$this->assign('list',$list);		
		$this->assign("title","权限管理");
		$this->display();

	}
	//添加用户组
	public function addGroup(){
		if(!IS_POST)$this->error("请求方式错误");
		$data = D('AuthGroup');
		if(!$data->create()){
			$this->error($data->getError());
		}else{
			$result=$data->add(I());
			$result?$this->success("删除成功"):$this->error("删除失败");
		}
		
	} 
	//编辑用户组
	public function editGroup(){
		$id = I("get.id");
		if(empty($id)) $this->error("缺少参数!");
		$this->group = M('auth_group')->where("id=".$id)->select();
		$this->assign("title","编辑用户组");
		$this->display();
	}
	//编辑用户组处理表单
	public function editGroupHandle(){
		if(!IS_POST) $this->error("提交方式错误!");
		$data = D("AuthGroup");
		if(!$data->create()){
			$this->error($data->getError());
		}else{
			$data->save(I()) ? $this->success("更新成功"):$this->error("更新失败");
		}

	}
	//删除用户组
	public function delGroup(){
		$this->error("此功能暂时禁止使用!");
	}
	//权限列表
	public function ruleList(){
   		$m=M('auth_group');
    	//获取当前组的所有规则id
    	$ruleID=$m->field('rules')->where("id=".I('id'))->select();
    	$rid =array();
    	foreach ($ruleID as $v) {
    		$rid[] = $v['rules'];
    	}
    	$rid = explode(",", $rid[0]);
    	//将每个module对应的权限存到多维数组
    	$rule=D("RuleView");
    	$mid=$rule->field('mid,module_name')->group('mid')->select();
    	$result=array();
    	foreach ($mid as $v) {
    		$v['rules'] = M('auth_rule')->field("id,title")->where("mid=".$v['mid'])->select();
    		$v['currentRID'] = $rid;
    		$result[] = $v;	
    	}
    	$this->result=$result;
    	$this->ruleID=$ruleID;
    	$this->groupID = I('get.id');
		$this->assign('title',"权限设置>".I('title'));
		$this->display();
	}
	//更新权限
	public function ruleListHandle(){
		if(!IS_POST)$this->error("提交方式错误!");
		if(I("post.id") == 1)$this->error("禁止修改超级管理员权限!");
		$rule = I('post.rule');
		$data['rules'] = implode(",", $rule);
		$data['id'] = I("post.id");
		$result = M('auth_group')->save($data);
		$result?$this->success("权限更新成功"):$this->error("权限更新失败");
	}	
}